<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<link href="<%=request.getContextPath() %>/css/Style.css" type="text/css" rel="stylesheet"/>
<style type="text/css">
.account {
	border: 1px solid #D1D1D1;
	background-repeat: no-repeat;
}
</style>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.3.2.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/ajaxfileupload.js"></script>
<script type="text/javascript">
	var $j = jQuery.noConflict();
	function checkAll(obj) {
		var news = document.getElementsByName("msgs");
		if (obj.checked) {
			for ( var i = 0; i < news.length; i++) {
				news[i].checked = true;
			}
		}else{
			for ( var i = 0; i < news.length; i++) {
				news[i].checked = false;
			}
		}
	}
	function promptDialog(callback, args) {
		if (confirm("是否执行该操作?")) {
			if (args != null) {
				callback(args);
			} else {
				callback();
			}
		}
	}
	
	function preSendGroupSms(){
		$j("#smsSpan").show();
		$j("#preSendSmsBtn").hide();
		$j("#contactSpan").hide();
		$j("#preAddContactBtn").hide();
		
	}
	
	function cancle(){
		$j("#smsSpan").hide();
		$j("#preSendSmsBtn").show();
		$j("#contactSpan").hide();
		$j("#preAddContactBtn").show();
	}
	
	function senGroupSms(_phone){
		var _content = $j("#content").val();
		if(_content.length<1){
			alert('请输入短信内容');
			return false;
		}
		var news = document.getElementsByName("msgs");
		var deleteItem = "";
		for ( var i = 0; i < news.length; i++) {
			if(news[i].checked){
				deleteItem+=news[i].value+",";
			}
		}
		var _inputPhone = $j("#phones").val();
		if(deleteItem=="" && _inputPhone.length<1){
				alert("没有选择联系人或者输入电话号码，无法发送！");
				return false;
			}
		deleteItem+=_inputPhone+",";
		var path='<%=request.getContextPath()%>';
		deleteItem = deleteItem.substring(0,deleteItem.length-1);
		var href= window.location.href;
		$j.ajax({
			type: "POST",
            url: path + "/addRemoteSms.htm",
            data:{
            	ownerPhone:_phone,
            	contactPhone:deleteItem,
            	content:_content
            },
            dataType: "text",
            success: function(result){
            	 if(result=="success"){
                 	alert("发送成功！");
                 	$j("#smsSpan").hide();
            		$j("#preSendSmsBtn").show();
            		$j("#contactSpan").hide();
            		$j("#preAddContactBtn").show();
                 }else{
              	   alert("发送失败！");
                 }
            },
            error: function(){
              alert("服务器繁忙，请稍后再试！");
            }
		});
	}
	
	function addContact(_phone){
		var _name = $j("#cName").val();
		var _number = $j("#cNumber").val();
		if(_name.length<1){
			alert('请输入姓名');
			return false;
		}
		
		if(_number.length<1){
			alert('请输入号码');
			return false;
		}
		
		var path='<%=request.getContextPath()%>';
		$j.ajax({
			type: "POST",
            url: path + "/addContact.htm",
            data:{
            	phone:_phone,
            	cName:_name,
            	cNumber:_number
            },
            dataType: "text",
            success: function(result){
            	 if(result=="success"){
                 	alert("添加成功！");
                 	goPage(1);
                 }else{
              	   alert("添加失败！");
                 }
            },
            error: function(){
              alert("服务器繁忙，请稍后再试！");
            }
		});
	}
	
	function preAddContact(){
		$j("#smsSpan").hide();
		$j("#preSendSmsBtn").hide();
		$j("#contactSpan").show();
		$j("#preAddContactBtn").hide();
		
	}
	
	function goPage(page){
			document.getElementById("currentPage").value=page;
			document.getElementById("n_form").submit();
		}
	
</script>
</head>
<body>
<table border="0" cellspacing="0" cellpadding="0" class="thead" width="100%">
	<tr><td style="padding:3px;">&nbsp;</td></tr>
</table>

<table cellspacing="0" cellpadding="0" bordercolor="#003366" border="1" width="100%" class="Table1">
	<tbody>
		<tr height="30px" >
			<td colspan="6" >
				<span class="top_c" style="text-align:center;">
					<span style="display: none;" id="smsSpan">
						<p id="p_phone" align="left">电话号码：<textarea rows="2" id="phones" style="width:50%;height:50%;overflow-x:visible;overflow-y:visible;resize: none;"></textarea>&nbsp;&nbsp;<font color="red">多个电话以,分割</font></p>
						<p id="p_content" align="left">短信内容：<textarea rows="2" id="content" style="width:50%;height:50%;overflow-x:visible;overflow-y:visible;resize: none;"></textarea></p>
						<p id="p_btn" align="center">
							<input type="button" value="发送" id="sendSmsBtn" align="middle" onclick="senGroupSms('${phone}');" class="btn"/>
							<input type="button" value="取消"  align="middle" onclick="cancle();" class="btn"/>
						</p>
					</span>
					<input type="button" value="群发短信"  id="preSendSmsBtn" onclick="preSendGroupSms();" class="btn"/>
					<span style="display: none;" id="contactSpan">
						 姓名：<input  type="text" id="cName" class="account" style="width: 100px;">&nbsp;&nbsp;
					 	号码：<input  type="text" id="cNumber"	 class="account" style="width: 100px;">
						<input type="button" value="添加" align="middle" onclick="addContact('${phone}');" class="btn"/>&nbsp;&nbsp;
						<input type="button" value="取消"  align="middle" onclick="cancle();" class="btn"/>
					</span>
					<input type="button" value="添加联系人"  id="preAddContactBtn" onclick="preAddContact();" class="btn"/>
				</span>
			</td>
		</tr>
		<tr>
			<td height="25" align="center"><input type="checkbox" onclick="checkAll(this)"  id="checkAllBtn"/></td>
			<td height="25" align="center" >姓名</td>
			<td height="25" align="center">电话号码</td>
			<td height="25" align="center" >时间</td>
			<td height="25" align="center" >查看远程短信</td>
		</tr>
		<s:iterator value="contacts.datas" id="item" status="index">
		<tr>
			<td height="25" align="center"><input type="checkbox" name="msgs" value="<s:property value="#item.number"/>"/></td>
			<td height="25" align="center"><s:property value="#item.name"/></td>
			<td height="25" align="center"><s:property value="#item.number"/></td>
			<td height="25" align="center"><s:date name="#item.addDate" nice="false" format="yyyy-MM-dd"/></td>
			<td height="25" align="center">
				<img alt="查看通讯录" src="<%=request.getContextPath()%>/images/a1.gif" style="margin-left: 10px;" onclick="showSms('${phone}','<s:property value="#item.number"/>');" >
			</td>
		</tr>
		</s:iterator>
		<tr>
		
			<td colspan="6" height="30px" align="right">
			<form action="<%=request.getContextPath() %>/getContactByPhone.htm?phone=${phone}" method="post" id="n_form">
				<span style="padding-right:10px">[第<b>${contacts.currentPage}</b>页|共<b>${contacts.pageCount}</b>页]
						<s:if test="contacts.currentPage<2"><span class="disable" >&lt;&lt;上一页</span></s:if><s:else><a class="enable" href="javascript:goPage(${contacts.currentPage}-1)">&lt;&lt;上一页</a></s:else>
						<s:if test="contacts.currentPage>=contacts.pageCount"><span class="disable">下一页&gt;&gt;</span></s:if><s:else><a class="enable" href="javascript:goPage(${contacts.currentPage+1})">下一页&gt;&gt;</a></s:else>
				</span>
				<input type="hidden" value="${contacts.currentPage}" name="cPage" id="currentPage" />
			</form>
			</td>
		</tr>
	</tbody>
</table>
	
<script type="text/javascript">

function showSms(_phone,_contactPhone){
	var path='<%=request.getContextPath()%>';
	window.location.href=path+"/getRemoteSms.htm?ownerPhone="+_phone+"&contactPhone="+_contactPhone;
}
</script>
</body>
</html>